
package com.sasis.webapp.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.sasis.Constants;
import com.sasis.model.ServiceSheet;
import com.sasis.service.ServiceSheetManager;
import com.sasis.webapp.form.ServiceSheetForm;

/**
 * Action class to handle CRUD on a ServiceSheet object
 *
 * @struts.action name="serviceSheetForm" path="/serviceSheets" scope="request"
 *  validate="false" parameter="method" input="mainMenu"
 * @struts.action name="serviceSheetForm" path="/editServiceSheet" scope="request"
 *  validate="false" parameter="method" input="list"
 * @struts.action name="serviceSheetForm" path="/saveServiceSheet" scope="request"
 *  validate="true" parameter="method" input="edit"
 * @struts.action-set-property property="cancellable" value="true"
 * @struts.action-forward name="edit" path="/WEB-INF/pages/serviceSheetForm.jsp"
 * @struts.action-forward name="list" path="/WEB-INF/pages/serviceSheetList.jsp"
 * @struts.action-forward name="search" path="/serviceSheets.html" redirect="true"
 */
public final class ServiceSheetAction extends BaseAction {
    public ActionForward cancel(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        return mapping.findForward("search");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'delete' method");
        }

        ActionMessages messages = new ActionMessages();
        ServiceSheetForm serviceSheetForm = (ServiceSheetForm) form;

        // Exceptions are caught by ActionExceptionHandler
        ServiceSheetManager mgr = (ServiceSheetManager) getBean("serviceSheetManager");
        mgr.removeServiceSheet(serviceSheetForm.getId());

        messages.add(ActionMessages.GLOBAL_MESSAGE,
                     new ActionMessage("serviceSheet.deleted"));

        // save messages in session, so they'll survive the redirect
        saveMessages(request.getSession(), messages);
        log.debug(">>>>>>>>>>>[serviceSheetForm request id]>>>>>>>>>>>"+serviceSheetForm.getRequestId());
        //updateFormBean(mapping, request, serviceSheetForm);
        return search(mapping,form,request,response);
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'edit' method");
        }

        String flag = request.getParameter("flag");
        String requestId = request.getParameter("taskId");
       
        ServiceSheetForm serviceSheetForm = (ServiceSheetForm) form;
       
        if("add".equals(flag)){
        	serviceSheetForm.reset();
        }
        // if an id is passed in, look up the user - otherwise
        // don't do anything - user is doing an add
        
        if (serviceSheetForm.getId() != null) {
            ServiceSheetManager mgr = (ServiceSheetManager) getBean("serviceSheetManager");
            ServiceSheet serviceSheet = mgr.getServiceSheet(serviceSheetForm.getId());
            serviceSheetForm = (ServiceSheetForm) convert(serviceSheet);
            
        }
        log.debug(">>>>>>>>>>>>>>>>>>[requestId1]>>>>>>>>>>"+requestId);
        
        copyObject(request, "requestId", requestId);
        return mapping.findForward("edit");
    }

    public ActionForward save(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'save' method");
        }

        // Extract attributes and parameters we will need 
        ServiceSheetForm serviceSheetForm = (ServiceSheetForm) form; 
        String requestId = null!=request.getParameter("taskId")?request.getParameter("taskId")
        		:getObject(request, "requestId"); 
        
        serviceSheetForm.setRequestId(requestId);
        
        log.debug(">>>>>>>>>>>>>>[requestId]>>>>>>>>>>>"+serviceSheetForm.getRequestId()); 
        ServiceSheetManager mgr = (ServiceSheetManager) getBean("serviceSheetManager");
        ServiceSheet serviceSheet = (ServiceSheet) convert(serviceSheetForm);
        log.debug(">>>>>>>>>>>>>>[ serviceSheet requestId]>>>>>>>>>>>"+serviceSheet.getRequestId());
        mgr.saveServiceSheet(serviceSheet);
        // add success messages 
        copyObject(request, "requestId", requestId);
        return saveForward(mapping,serviceSheetForm,request,response,null);
    }

    public ActionForward search(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'search' method");
        }
        
        ServiceSheetForm serviceSheetForm = (ServiceSheetForm) form;
        log.debug(">>>>>>>>>>>>>>>>>[request id]>>>>>>>>>>>>"+serviceSheetForm.getRequestId());
        String requestId = null!=request.getParameter("taskId")?request.getParameter("taskId")
        		:getObject(request, "requestId"); 
        serviceSheetForm.setRequestId(requestId);
        ServiceSheet serviceSheet = (ServiceSheet) convert(serviceSheetForm);

        ServiceSheetManager mgr = (ServiceSheetManager) getBean("serviceSheetManager");
        request.setAttribute(Constants.SERVICESHEET_LIST, mgr.getServiceSheets(serviceSheet));
        
        return mapping.findForward("list");
    }
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
            throws Exception {
        return search(mapping, form, request, response);
    }
}
